package com.cloud.service.Impl;

import com.cloud.dao.TeamDao;
import com.cloud.dto.TeamDto;
import com.cloud.dto.UserDto;
import com.cloud.entity.Team;
import com.cloud.entity.User;
import com.cloud.service.TeamService;
import com.cloud.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TeamServiceImpl implements TeamService {
    @Autowired
    TeamDao teamDao;
    @Autowired
    UserService userService;

    @Override
    public TeamDto addTeam(TeamDto teamDto){
        Team team=new Team();
        BeanUtils.copyProperties(teamDto,team);
        team=teamDao.save(team);
        BeanUtils.copyProperties(team,teamDto);
        return teamDto;
    }

    @Override
    public List<UserDto> getMyMember(Integer userId){
        UserDto userDto=userService.getUerById(userId);
        Integer teamId=userDto.getTeamId();
        List<UserDto> userDtoList=userService.getUserByTeamId(teamId);
        return userDtoList;
    }

    @Override
    public TeamDto getTeamById(Integer teamId){
        Team team=teamDao.getOne(teamId);
        TeamDto teamDto=new TeamDto();
        BeanUtils.copyProperties(team,teamDto);
        return teamDto;
    }

    @Override
    public TeamDto getTeamByName(String teamName){
        Team team=teamDao.findTeamByTeamName(teamName);
        TeamDto teamDto=new TeamDto();
        BeanUtils.copyProperties(team,teamDto);
        return teamDto;
    }

//    @Override
//    public List<TeamDto> addLeader_name(List<TeamDto> teamDtoList){
//        int n=teamDtoList.size();
//        List<TeamDto> teamDtos=new ArrayList<>();
//        for(int i=0;i<n;i++){
//            Team team=new Team();
//            TeamDto teamDto=teamDtoList.get(i);
//            int leader_id=teamDto.getUserId();
//            UserDto userDto=userService.getUerById(leader_id);
//            teamDto.setLeaderName(userDto.getName());
//            BeanUtils.copyProperties(teamDto,team);
//            teamDao.save(team);
//            teamDtos.add(teamDto);
//        }
//        return teamDtos;
//    }

    @Override
    public List<TeamDto> getAllTeam(){
        List<Team> teamList=teamDao.findAll();
        List<TeamDto> teamDtoList=new ArrayList<>();
        for(int i=0;i<teamList.size();i++){
            TeamDto teamDto=new TeamDto();
            Team team=teamList.get(i);
            BeanUtils.copyProperties(team,teamDto);
            teamDtoList.add(teamDto);
        }
        return teamDtoList;
    }
}
